package com.runvp.common.util.enc;

import com.runvp.common.hex.HexConvert;

import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

public class AESGCMDecryptor {
    public static void main(String[] args) {
        try {
            // 密钥和初始化向量应该是安全的随机字节，通常使用密钥生成函数生成
            byte[] keyBytes = "BMrcujdO0XDjvOOa".getBytes(StandardCharsets.UTF_8); // 128位密钥
            byte[] ivBytes = HexConvert.str2HexStr("98BPRR4Wve41").getBytes(StandardCharsets.UTF_8); // 128位IV
            SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
            GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(128, ivBytes);

            // 解密数据（Base64编码）
            String encryptedData = "6BE3153AFC79025ACDF03CB38D988927BBF1DB4FCE387729CB19AB13FEC136";
            byte[] encryptedBytes = HexStringToASCII.hexToAsciiByte(encryptedData);

            // 创建解密Cipher
            Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmParameterSpec);
            cipher.updateAAD(HexStringToASCII.hexToAsciiByte("7130C4C92F60B7EB4D565B1DDFC2559E"));
            // 执行解密
            byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
            String decryptedData = new String(decryptedBytes, StandardCharsets.UTF_8);

            System.out.println("解密后的数据: " + decryptedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}